package com.campus.secondhand.controller;

import java.util.List;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.campus.secondhand.common.Result;
import com.campus.secondhand.entity.School;
import com.campus.secondhand.model.vo.PurchaseRequestVO;
import com.campus.secondhand.model.vo.SchoolUserStatusVO;
import com.campus.secondhand.model.vo.ProductListVO;
import com.campus.secondhand.service.PurchaseRequestService;
import com.campus.secondhand.service.SchoolService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 学校信息控制器
 */
@RestController
@RequestMapping("/schools")
@RequiredArgsConstructor
@Slf4j
public class SchoolController {

    private final SchoolService schoolService;
    private final PurchaseRequestService purchaseRequestService;

    /**
     * 获取所有学校列表
     * 
     * @return 学校列表
     */
    @GetMapping("/list")
    public Result<List<School>> getAllSchools() {
        return schoolService.getAllSchools();
    }

    /**
     * 根据ID获取学校信息
     * 
     * @param id 学校ID
     * @return 学校信息
     */
    @GetMapping("/{id}")
    public Result<School> getSchoolById(@PathVariable Long id) {
        return schoolService.getSchoolById(id);
    }

    /**
     * 根据名称搜索学校
     * 
     * @param name 学校名称（支持模糊搜索）
     * @return 学校列表
     */
    @GetMapping("/search")
    public Result<List<School>> searchSchoolsByName(@RequestParam String name) {
        return schoolService.searchSchoolsByName(name);
    }

    /**
     * 获取用户与指定学校的关系状态
     * 
     * @param id     学校ID
     * @param userId 用户ID
     * @return 用户与学校的关系状态
     */
    @GetMapping("/{id}/user-status")
    public Result<SchoolUserStatusVO> getUserSchoolStatus(
            @PathVariable Long id,
            @RequestParam(required = false) Long userId) {
        return schoolService.getUserSchoolStatus(id, userId);
    }

    /**
     * 获取指定学校的商品列表
     * 
     * @param id   学校ID
     * @param page 页码，默认1
     * @param size 每页数量，默认10
     * @return 商品列表分页结果
     */
    @GetMapping("/{id}/products")
    public Result<Page<ProductListVO>> getSchoolProducts(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return schoolService.getSchoolProducts(id, page, size);
    }

    /**
     * 获取学校的求购列表
     * 
     * @param id   学校ID
     * @param page 页码，默认1
     * @param size 每页数量，默认12
     * @return 学校求购列表
     */
    @GetMapping("/{id}/wanted")
    public Result<org.springframework.data.domain.Page<PurchaseRequestVO>> getSchoolRequests(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "12") Integer size,
            @RequestParam(required = false) Long userId) {

        // 首先验证学校是否存在
        Result<?> schoolResult = schoolService.getSchoolById(id);
        if (schoolResult.getCode() != 200) {
            return Result.error(schoolResult.getCode(), schoolResult.getMessage());
        }

        // 构建分页参数
        org.springframework.data.domain.Pageable pageable = PageRequest.of(page - 1, size,
                Sort.by("createTime").descending());

        // 调用服务获取求购列表，不传筛选参数，筛选由前端完成
        return purchaseRequestService.getSchoolRequests(id, null, null, null, pageable, userId);
    }
}